package com.frxuexi.sign

import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.sign.databinding.ItemSignBinding

class SignAdapter : BaseQuickAdapter<SignBeanItem, BaseViewHolder>(R.layout.item_sign) {
    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        super.onItemViewHolderCreated(viewHolder, viewType)
        DataBindingUtil.bind<ItemSignBinding>(viewHolder.itemView)
    }

    override fun convert(helper: BaseViewHolder, item: SignBeanItem) {
        val binding = DataBindingUtil.getBinding<ItemSignBinding>(helper.itemView)!!
        binding.bean = item
        var date = ""
        var start = ""
        var end = ""
        if (item.morningCheckIn.isNotEmpty()) {
            val split = item.morningCheckIn.split(" ")
            date = split[0].replaceFirst("-", "年").replaceFirst("-", "月").plus("日")
            if (split.size == 2) {
                start = split[1]
            }
        }
        if (item.afternoonCheckIn.isNotEmpty()) {
            val split = item.afternoonCheckIn.split(" ")
            if (split.size == 2) {
                end = split[1]
            }
        }
        binding.signDate.setText(date)
        binding.signTime.setText("上班:${start}-下班：${end}")
        val split = item.status.split(",")
        if(split.size==1){
            binding.signStatusStart.setText(split[0])
            setColor(binding.signStatusStart, split[0])
        }else if(split.size==2){
            binding.signStatusEnd.setText(split[1])
            setColor(binding.signStatusEnd, split[1])
        }
    }

    fun setColor(tv: TextView, status: String) {
        tv.apply {
            when (status) {
                "迟到" -> {
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_weihu)
                    setTextColor(context.getColor(com.frxuexi.lib_base.R.color.darkorange))
                }

                "早退" -> {
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_red)
                    setTextColor(context.getColor(com.frxuexi.lib_base.R.color.redfont))
                }

                else -> {
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_status)
                    setTextColor(context.getColor(com.frxuexi.lib_base.R.color.green))
                }
            }

        }
    }
}